<?php
namespace backend\controllers;

use backend\models\ClueOrgin;
use backend\models\CompanyCats;
use backend\models\CoordinationDegree;
use backend\models\Departments;
use backend\models\FollowInfos;
use backend\models\FollowType;
use backend\models\Region;
use Yii;
use yii\data\Pagination;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use common\models\Admin;
use backend\models\Clue;
use backend\models\EnterpriseGrade;
use backend\models\ProductStandard;

/**
 * Clue controller
 */
class ClueController extends Controller
{
    public $enableCsrfValidation = false;//开启防止crf攻击
    //public $layout='base';
    /**
     * @inheritdoc
     */

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }


    /**
     * 线索首页
     */
    public function actionIndex()
    {
        //$clueList=Clue::getClueList();
        checkGrant('ckallclue');
        $provinceList=Region::getProvinceList();
        $citiesList=Region::getCitiesList();
        $clueOrginList=ClueOrgin::getClueOrgin();
        $companyCats=CompanyCats::getCompanyCatsList();
        $coordinationList=CoordinationDegree::getCoordinationDegreeList();
        $gradeList=EnterpriseGrade::getEnterpriseGrade();
        $clueList=Clue::find()->joinWith('clueOrgin')->joinWith('followType')->joinWith('coordinationDegree')->joinWith('users')->joinWith('province')->joinWith('city');

        $followTypeList=FollowType::getFollowTypeList();
        $departmentsList=Departments::getAllDeparmentList();
        $page=new Pagination(['totalCount'=>$clueList->count()]);
        $page->setPageSize(2);
        $clueList=$clueList->offset($page->offset)->limit($page->limit)->asArray()->all();
        $data=array();
        $data['page']=$page;
        $data['clueList']=$clueList;
        $data['provinceList']=$provinceList;
        $data['citiesList']=$citiesList;
        $data['clueOrginList']=$clueOrginList;
        $data['companyCats']=$companyCats;
        $data['coordination']=$coordinationList;
        $data['gradeList']=$gradeList;
        $data['followTypeList']=$followTypeList;
        $data['departmentsList']=$departmentsList;


        return $this->renderPartial('index',$data);
    }
    /**
     * 添加线索操作
     */
    public function actionAddClued(){
        $params=\Yii::$app->request->post();
        $companyName=getData($params['companyName'],'请填写公司名称');
        $provinceId=getData($params['provinceId'],'请选择省份');
        $cityId=getData($params['cityId'],'请选择城市');
        $companyAddress=getData($params['companyAddress'],'请填写公司地址');
        $companyType=getData($params['companyType'],'公司类型不能为空');
        $departmentId=getData($params['departmentId'],'请选择负责部门');
        //$responUserId=getData($params['responUserId'],'请选择负责客服');
        $contactName=getData($params['contactName'],'请填写联系人');
        $job=getData($params['job'],'请填写职务');
        $phone=getData($params['phone'],'请填写联系电话');
        $cellPhone=getData($params['cellphone'],'请填写手机号码');
        $wechatId=getData($params['wechatId'],'请填写微信号');
        $mainProduct=getData($params['mainProduct'],'请填写主营产品');
        $qq=getData($params['QQ'],请填写qq);
        $id=$params['clueId'];

        $checkName=Clue::findOne(['companyName'=>$companyName]);
      /*  if(!empty($checkName)){
            returnResult(100020,'公司名称已存在')
        }*/
        if($id>0){
            $clue=Clue::findOne(['id'=>$id]);
            if(empty($clue)){
                returnResult(100019,'线索记录不存在');
            }
        }else{
            $clue=new Clue();
        }
        $clue->companyName=$companyName;
        $clue->companyAddress=$companyAddress;
        $clue->mainProduct=$mainProduct;
        $clue->standardId=2;
        $clue->contactName=$contactName;
        $clue->job=$job;
        $clue->phone=$phone;
        $clue->cellPhone=$cellPhone;
        $clue->weChatId=$wechatId;
        $clue->QQ=$qq;
        $clue->email=$params['email'];
        $clue->responUserId=$responUserId;
        $clue->followStatus=1;
        $clue->coordinationStatus=1;
        $clue->clueOrgin=2;
        $clue->lastFollowTime=strtotime(date('Y-m-d'));
        $clue->nextFollowTime=strtotime(date('Y-m-d'));
        $clue->addTime=time();
        $clue->provinceId=$provinceId;
        $clue->cityId=$cityId;
        $doResult=$clue->save();
        if($doResult){
            returnResult(0,'操作线索成功');
        }else{
            returnResult(100021,'操作线索失败');
        }




    }
    /**
     * 删除线索
     */
    public function actionDeleteClue(){
        $params=Yii::$app->request->post();
        $clue=Clue::findOne(['id'=>$params['clueId']]);
        if(empty($clue)){
            returnResult(100040,'无法删除该记录');
        }
        $doResult=$clue->delete();
        if($doResult){
            returnResult(0,'删除成功');
        }else{
            returnResult(100041,'删除失败');
        }
    }
    /**
     * 添加线索页面
     */
    public function actionAddClueShow(){
        $provinceList=Region::getProvinceList();
        $defaultCitiesList=Region::getCitiesList($provinceList[0]['code']);
        var_dump($defaultCitiesList);
        die;
        return $this->renderPartial('add');
    }

    /**
     *线索详情页
     */
    public function actionClueDetail(){
        $params=\Yii::$app->request->get();
        $id=$params['clueId'];
        $coordinationList=CoordinationDegree::getCoordinationDegreeList();
        $followTypeList=FollowType::getFollowTypeList(3);
        $followStatusList=FollowType::getFollowTypeList(2);
        $clueInfo=Clue::find()->joinWith('clueOrgin')->joinWith('followType')->joinWith('coordinationDegree')->joinWith('users')->where(['al_clue.id'=>$id])->asArray()->one();;
        $followInfosList=FollowInfos::find()->joinWith('followType')->joinWith('followStatus')->joinWith('coordinationDegree')->joinWith('users')->where(['al_follow_infos.clueId'=>$id,'al_follow_infos.type'=>1])->orderBy('id DESC')->asArray()->all();

        $data=['clueInfo'=>$clueInfo,'followInfosList'=>$followInfosList];
        $data['coordinationList']=$coordinationList;
        $data['followTypeList']=$followTypeList;
        $data['followStatusList']=$followStatusList;
        $data['standardList']=ProductStandard::getStandardList();

        $provinceList=Region::getProvinceList();
        $citiesList=Region::getCitiesList();
        $clueOrginList=ClueOrgin::getClueOrgin();
        $companyCats=CompanyCats::getCompanyCatsList();
        $coordinationList=CoordinationDegree::getCoordinationDegreeList();
        $gradeList=EnterpriseGrade::getEnterpriseGrade();
        $followTypeList=FollowType::getFollowTypeList();
        $departmentsList=Departments::getAllDeparmentList();
        $data['provinceList']=$provinceList;
        $data['citiesList']=$citiesList;
        $data['clueOrginList']=$clueOrginList;
        $data['companyCats']=$companyCats;
        $data['coordination']=$coordinationList;
        $data['gradeList']=$gradeList;
        $data['followTypeList']=$followTypeList;
        $data['departmentsList']=$departmentsList;

        return $this->render('clue-detail',$data);
    }
    /**
     * 指派线索的负责人
     */
    public function actionAssignClue(){
        $params=Yii::$app->request->post();
        $clueId=getData($params['clueId'],'记录不存在');
        $responUserId=getData($params['responUserId'],'请选择负责人');
        $checkClue=Clue::findOne(['id'=>$clueId]);
        if(empty($checkClue)){
            returnResult(100043,'找不到该记录');
        }
        $checkClue->responUserId=$responUserId;
        $doResult=$checkClue->save();
        if($doResult){
            returnResult(0,'指派成功');
        }else{
            returnResult(100044,'指派失败');
        }
    }


}
